local skel = fk.CreateSkill {
  name = "lb__jianzhi",
}

Fk:loadTranslationTable {
  ["lb__jianzhi"] = "剑制",
  [":lb__jianzhi"] = "你可以获得其他角色或弃置自己装备区内一张武器牌：<br>" ..
      "1.视为使用【杀】或【无懈可击】；<br>" ..
      "2.令一张【杀】无效。<br>" ..
      "两项均触发后，你视为装备着场上及弃牌堆中所有的武器牌。",


  ["#lb__jianzhi"] = "剑制：你可以获得其他角色或弃置自己装备区内一张武器牌，视为使用【杀】或【无懈可击】",
  ["#lb__jianzhi_slash"] = "剑制：你可以获得其他角色或弃置自己装备区内一张武器牌，令 %arg 无效",

  ["@lb__jianzhi"] = "剑制",
  ["@$lb__jianzhi"] = "剑制",

  ["$lb__jianzhi1"] = "I am the bone of my sword.",
  ["$lb__jianzhi2"] = "So as I pray— Unlimited Blade Works!",
  ["$lb__jianzhi3"] = "别想跑！Caladbolg II!",
  ["$lb__jianzhi4"] = "Trace on!",
}

skel:addEffect("viewas", {
  pattern = "slash,nullification",
  prompt = "#lb__jianzhi",
  expand_pile = function(self, player)
    local ids = table.filter(player:getTableMark("lb__jianzhi"), function(id)
      return not table.contains(player:getCardIds("e"), id)
    end)
    return ids
  end,
  interaction = function(self, player)
    local all_names = { "slash", "nullification" }
    return UI.CardNameBox {
      choices = player:getViewAsCardNames(skel.name, all_names),
      all_choices = all_names,
    }
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected > 0 or self.interaction.data == nil then return false end
    return table.contains(player:getTableMark("lb__jianzhi"), to_select)
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or self.interaction.data == nil then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skel.name
    card.extra_data = card.extra_data or {}
    card.extra_data.lb__jianzhi = cards[1]
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local data = use.card.extra_data or {}
    local success = false
    if data.lb__jianzhi then
      local id = data.lb__jianzhi
      if table.contains(player:getCardIds("e"), id) then
        room:throwCard(id, skel.name, player, player)
        success = not table.contains(player:getCardIds("e"), id)
      else
        local t = room:getCardOwner(id)
        room:obtainCard(player, id, true, fk.ReasonPrey, player, skel.name)
        if t then
          success = not table.contains(t:getCardIds("e"), id)
        end
      end
    end
    if not success then
      return skel.name
    end
    if #player:getTableMark("@lb__jianzhi") < 2 then
      room:addTableMarkIfNeed(player, "@lb__jianzhi", "①")
      if #player:getTableMark("@lb__jianzhi") > 1 then
        skel:onAcquire(player, false)
      end
    end
  end,
  enabled_at_play = function(self, player)
    return #player:getTableMark("lb__jianzhi") > 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and #player:getTableMark("lb__jianzhi") > 0
  end,
})

skel:addEffect(fk.CardUsing, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and data.card.trueName == "slash" and #player:getTableMark("lb__jianzhi") > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local ids = player:getTableMark("lb__jianzhi") ---@cast ids integer[]
    local pile = table.filter(ids, function(id)
      return not table.contains(player:getCardIds("e"), id)
    end)
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      expand_pile = pile,
      pattern = tostring(Exppattern { id = ids }),
      cancelable = true,
      prompt = "#lb__jianzhi_slash:::" .. data.card:toLogString(),
      include_equip = true,
    })
    event:setCostData(self, { cards = cards })
    return #cards > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local id = event:getCostData(self).cards[1]
    local success = false
    if table.contains(player:getCardIds("e"), id) then --无法弃置/获得即发动失败
      room:throwCard(id, skel.name, player, player)
      success = not table.contains(player:getCardIds("e"), id)
    else
      local t = room:getCardOwner(id)
      room:obtainCard(player, id, true, fk.ReasonPrey, player, skel.name)
      if t then
        success = not table.contains(t:getCardIds("e"), id)
      end
    end
    if success then
      if #player:getTableMark("@lb__jianzhi") < 2 then
        room:addTableMarkIfNeed(player, "@lb__jianzhi", "②")
        if #player:getTableMark("@lb__jianzhi") > 1 then
          skel:onAcquire(player, false)
        end
      end
      data.toCard = nil --令牌失效
      data.tos = {}
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name, true, true) then
      for _, move in ipairs(data) do
        if move.toArea == Card.PlayerEquip or move.toArea == Card.DiscardPile then
          return true
        end
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip or info.fromArea == Card.DiscardPile then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    skel:onAcquire(player, false)
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  local ids = {}
  local dis = {}
  for _, t in ipairs(room:getAlivePlayers()) do
    for _, id in ipairs(t:getCardIds("e")) do
      if Fk:getCardById(id).sub_type == Card.SubtypeWeapon then
        table.insert(ids, id)
      end
    end
  end
  for _, id in ipairs(room.discard_pile) do
    if Fk:getCardById(id).sub_type == Card.SubtypeWeapon then
      table.insert(dis, id)
    end
  end
  room:setPlayerMark(player, "lb__jianzhi", ids)
  room:setPlayerMark(player, "lb__jianzhi_dis", dis)
  if #player:getTableMark("@lb__jianzhi") > 1 then
    room:setPlayerMark(player, "@$lb__jianzhi", dis)
    local weapon = {}
    table.insertTable(weapon, ids)
    table.insertTableIfNeed(weapon, dis)
    for _, e in ipairs(weapon) do
      local card = Fk:getCardById(e) ---@cast card EquipCard
      local skills = card:getEquipSkills(player)
      for _, s in ipairs(skills) do
        if not player:hasSkill(s, true) then  --目标没有的技能
          room:handleAddLoseSkills(player, s.name, nil, false, true)
        end
      end
    end
    for _, skill in ipairs(player:getAllSkills()) do
      local sk = skill:getSkeleton()
      if sk and sk.attached_equip and not table.find(weapon, function(id)
            return Fk:getCardById(id).name == sk.attached_equip
          end) then
        local we = Fk:cloneCard(sk.attached_equip) ---@cast we EquipCard
        if we.sub_type == Card.SubtypeWeapon then
          local skills = we:getEquipSkills(player)
          for _, s in ipairs(skills) do
            if player:hasSkill(s, true) then
              room:handleAddLoseSkills(player, "-" .. s.name, nil, false, true)
            end
          end
        end
      end
    end
  end
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@lb__jianzhi", 0)
end)

return skel
